package com.ruoyi.web.controller.snmp;

import com.ruoyi.system.domain.SnmpDeviceinfo;
import com.ruoyi.system.domain.SnmpSensorinfo;
import com.ruoyi.system.domain.SnmpSensorinfohistory;
import com.ruoyi.system.service.ISnmpDeviceinfoService;
import com.ruoyi.system.service.ISnmpSensorinfoService;
import com.ruoyi.system.service.ISnmpSensorinfohistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Component("senserTask")
public class snmpSensorRunApplication {
    @Autowired
    private ISnmpSensorinfoService snmpSensorinfoService;
    @Autowired
    private ISnmpDeviceinfoService snmpDeviceinfoService;
    @Autowired
    private ISnmpSensorinfohistoryService snmpSensorinfohistoryService;

    private final WebClient webClient;
    public snmpSensorRunApplication(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.build();
    }
    public void GetDeviceInfo(){
        System.out.println("Start:"+new Date()+"  【获取设备传感器定时任务开始执行】");
        SnmpDeviceinfo snmpDeviceinfo=new SnmpDeviceinfo();
        List<SnmpDeviceinfo>  deviceArry=snmpDeviceinfoService.selectSnmpDeviceinfoList(snmpDeviceinfo);
        for (SnmpDeviceinfo deviceItem:deviceArry) {
            GetSenserInfo(deviceItem);
        }
        System.out.println("End:"+new Date()+"  【获取设备传感器定时任务结束执行】");
    }
    public void GetSenserInfo(SnmpDeviceinfo deviceinfoEntity)
    {
        SnmpSensorinfo snmpSensorinfo=new SnmpSensorinfo();
        snmpSensorinfo.setDeviceId(deviceinfoEntity.getDeviceId());
        List<SnmpSensorinfo> list = snmpSensorinfoService.selectSnmpSensorinfoList(snmpSensorinfo);

        String urlstr = "http://" + deviceinfoEntity.getDeviceIp();
        CompletableFuture<List<SnmpSensorinfo>> futures =  fetchSensorDataAsync(list,urlstr);

        // 等待所有异步任务完成
        List<SnmpSensorinfo> resultList = futures.join();
        list=resultList;
        //将值保存到历史信息表中
        SnmpSensorinfohistory snmpSensorinfohistory;
        for (SnmpSensorinfo entityhistory:list) {
            if(entityhistory.getSensorInfo()!=null&&entityhistory.getSensorInfo().trim().length()>0) {
                snmpSensorinfohistory = new SnmpSensorinfohistory();
                snmpSensorinfohistory.setDeviceId(entityhistory.getDeviceId());
                snmpSensorinfohistory.setSensorId(entityhistory.getSensorId());
                snmpSensorinfohistory.setSensorValue(entityhistory.getSensorInfo());
                snmpSensorinfohistory.setCreateTime(new Date());
                snmpSensorinfohistoryService.insertSnmpSensorinfohistory(snmpSensorinfohistory);
            }
        }

    }
    public CompletableFuture<List<SnmpSensorinfo>> fetchSensorDataAsync(List<SnmpSensorinfo> arrysensorList,String urlstr) { // 构建请求URL

        return webClient.get()
                .uri(urlstr)
                .retrieve()
                .bodyToMono(Map.class)
                .map(response -> {
                    if (response != null) {
                        for (SnmpSensorinfo item:arrysensorList) {
                            if ( response.containsKey(item.getSensorGetdata())) {
                                item.setSensorInfo(response.get(item.getSensorGetdata()).toString());
                            } else {
                                System.out.println("无法找到字段 " + item.getSensorGetdata() + " 的值");
                            }
                        }
                    }
                    return arrysensorList; // 返回修改后的 item
                })
                .onErrorResume(error -> {
                    System.err.println("请求失败: " + error.getMessage());
                    return Mono.just(arrysensorList); // 发生错误时返回原始 item
                })
                .toFuture(); // 将 Mono 转换为 CompletableFuture
    }

}
